﻿using System;
using System.Collections.Generic;
using OpenQA.Selenium;

namespace Selenium.PageToolkit.ControlFactoryChainImpl
{
    internal class WebElementCollectionFactory : ChainBase<PageElementAttribute>
    {
        protected override object Inject(Type fieldType, PageElementAttribute attribute, LambdaPageFactory factory)
        {
            if (!fieldType.IsAssignableFrom(typeof(IList<IWebElement>)))
                return null;
            
            return LambdaPageFactory.Generator.CreateInterfaceProxyWithoutTarget(typeof(IList<IWebElement>),
                                                                                 new WebElementCollectionInterceptor(() => factory.DriverFactory().FindElements(attribute.Spec()),
                                                                                                                     typeof(IWebElement)));
        }
    }
}